package com.ldj.struts.action;


import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ldj.common.MenuBuilder;
import com.ldj.common.SystemConfig;
import com.ldj.common.SystemSession;
import com.ldj.hibernate.RoleInfoDAO;
import com.ldj.hibernate.RolePowerInfo;
import com.ldj.hibernate.RolePowerInfoDAO;
import com.ldj.hibernate.SysLogInfo;
import com.ldj.hibernate.SysLogInfoDAO;

public class RolePowerInfoAction extends DispatchAction {

	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		RolePowerInfoDAO dao = new RolePowerInfoDAO(request,response);
		//RolePowerInfo info = new RolePowerInfo();
		String rid = request.getParameter("rid");
		RoleInfoDAO rDAO = new RoleInfoDAO(request,response);
		String username = rDAO.findById(rid).getRoleInfoName();
		String[] code = request.getParameterValues("functionMenuCode");
		String codeString = "";
		if(code != null && code.length > 0)
			for(int i=0;i<code.length;i++){
				 codeString+=code[i] + ",";
			}
		if(codeString.length() > 0)
			codeString = codeString.substring(0, codeString.length() - 1);
		
		
		//String codeString = Arrays.toString(code);
		List<RolePowerInfo> infoList = dao.findByCode(rid);
		RolePowerInfo info = new RolePowerInfo();
		if(infoList != null && infoList.size()>0){
			RolePowerInfo info1 =infoList.get(0);
			info.setRolePowerInfoRoleId(rid);
			info.setRolePowerInfoMenuId(codeString);
			dao.delete(info1);
		}else{
			info.setRolePowerInfoRoleId(rid);
			info.setRolePowerInfoMenuId(codeString);
		}
		dao.save(info);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("4");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "RolePowerInfo").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", username));
		logDao.save(s);
		return new ActionForward("/RoleInfo.do?method=list", true);
	}
	
	public ActionForward tree(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String value = "";
		RolePowerInfoDAO dao1 = new RolePowerInfoDAO(request,response);
		String rid = request.getParameter("rid");
		List<RolePowerInfo> infoList = dao1.findByCode(rid);
		if(infoList != null && infoList.size()>0){
			RolePowerInfo info1 =infoList.get(0);
			value = info1.getRolePowerInfoMenuId();
		}
		if(value == null)
			value = "";
		MenuBuilder bu = new MenuBuilder();
		request.setAttribute("tree", bu.getFunctionMenuTree(request, response, "功能菜单", "", "", value));	
		RoleInfoDAO dao=new RoleInfoDAO(request,response);
		String username=dao.findById(rid).getRoleInfoName();
		request.setAttribute("username", username);
		return mapping.findForward("tree");
	}
}